class UTVWeap_Rockets extends UTVehicleWeapon HideDropDown;

var int AmmoCount;

/**
 * GetAdjustedAim begins a chain of function class that allows the weapon, the pawn and the controller to make
 * on the fly adjustments to where this weapon is pointing.
 */
simulated function Rotator GetAdjustedAim( vector StartFireLoc )
{
	local rotator R;

	// Start the chain, see Pawn.GetAdjustedAimFor()
	R = Instigator.GetAdjustedAimFor( Self, StartFireLoc );

	if ( PlayerController(Instigator.Controller) != None )
	{
		R.Pitch = R.Pitch & 65535;
		if ( R.Pitch < 16384 )
		{
			R.Pitch += (16384 - R.Pitch)/16;
		}
		else if ( R.Pitch > 49152 )
		{
			R.Pitch += 1024;
		}
	}
	else
	{
		// due to the way SuggestTossVelocity() tests in increments combined with the high projectile speed,
		// the bots tend to overshoot just a tiny bit, so we nudge their aim down a little here
		R.Pitch -= 100;
	}

	return R;
}

simulated function Projectile ProjectileFire()
{
	local UTProj_HomingRocket SpawnedRocket;
	local UTProj_Shield SpawnedShield;
	local Vector s;

	local Actor target;
	//`log(AmmoCount);
	if(AmmoCount<=0)
		return none;
	AmmoCount--;

	s = MyVehicle.Location;
	s.Z += 100;

	//IncrementFlashCount();
	if(RaceCar(Owner).selectedWeapon == 1)
	{
		target = FindNearestEnemy();
		if(target == none){
			AmmoCount++;
			return none;	
		}
		

		SpawnedRocket = Spawn(class 'UTProj_HomingRocket',,, s);
		SpawnedRocket.Enemy = target;
		SpawnedRocket.Shooter = Owner;
		SpawnedRocket.Init( vector(AddSpread(MyVehicle.WeaponRotation)));
		RaceCar(Owner).Ammo = AmmoCount;
		return SpawnedRocket;
	}
	else if(RaceCar(Owner).selectedWeapon == 2)
	{
		if(RaceCar(Owner).shieldActivated)
		{
			AmmoCount++;
			return none;
		}
		SpawnedShield = Spawn(class 'UTProj_Shield',,, s);
		SpawnedShield.Shooter = Owner;
		SpawnedShield.time = 10;
		RaceCar(Owner).activateShield(10);
		RaceCar(Owner).Ammo = AmmoCount;
		return SpawnedShield;
	}
	return none;
}

function Actor FindNearestEnemy()
{
	local Controller C;
	local float prevDistance,currentDistance;
	local bool first;
	local Pawn target;
	first = true;

	foreach WorldInfo.AllControllers(class'Controller', C)
	{
		if (C.bIsPlayer && !C.IsDead() && C.Pawn != Owner)
		{
			currentDistance = VSize(C.Pawn.Location - Location);
			if( currentDistance < prevDistance || first)
			{
				if(FastTrace(Location,C.Pawn.Location))
				{
					first = false;
					prevDistance= currentDistance;
					target = C.Pawn;
				}
			}
		}
	}

	return target;
}

defaultproperties
{
	WeaponColor=(R=64,G=255,B=64,A=255)
	PlayerViewOffset=(X=11.0,Y=7.0,Z=-9.0)

	FireInterval(0)=2;
	WeaponFireTypes(0)=EWFT_Projectile
	WeaponProjectiles(0)=class'UTProj_HomingRocket'

	bFastRepeater=true

	bInstantHit=false
	bSplashJump=false
	bRecommendSplashDamage=false
	bSniping=false
	ShouldFireOnRelease(0)=0
	ShotCost(0)=0
	ShotCost(1)=0

	FireOffset=(X=19,Y=10,Z=-10)
	IconX=382
	IconY=82
	IconWidth=27
	IconHeight=42
	VehicleClass=class'RaceCar'
	WeaponRange=7000
	AimError=650
	AmmoCount = 0;
}

